package com.uestc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.uestc.filter.MyFilter.MyContactUsFilter;
import com.uestc.filter.MyFilter.MyLoginFilter;
import com.uestc.mapper.ContactUsMapper;
import com.uestc.mapper.RelatedLinkMapper;
import com.uestc.pojo.ContactUsExample;
import com.uestc.pojo.RelatedLinkExample;

@Configuration
public class MyFilter {
	@Autowired
	private ContactUsMapper contactUsMapper;
	@Autowired
	private RelatedLinkMapper relatedLinkMapper;

	@Bean
	public RemoteIpFilter remoteIpFilter() {
		return new RemoteIpFilter();
	}

	@Bean
	public FilterRegistrationBean<MyLoginFilter> myLoginFilterRegistration() {
		FilterRegistrationBean<MyLoginFilter> registration = new FilterRegistrationBean<MyLoginFilter>();
		registration.setFilter(new MyLoginFilter());
		registration.setName("myLoginFilter");
		registration.addUrlPatterns("/admin/aboutus/*");
		registration.addUrlPatterns("/admin/slider");
		registration.addUrlPatterns("/admin/services/*");
		registration.addUrlPatterns("/admin/news/*");
		registration.addUrlPatterns("/admin/addNews");
		registration.addUrlPatterns("/admin/addnews");
		registration.addUrlPatterns("/admin/delNews/*");
		registration.addUrlPatterns("/admin/updateNews/*");
		registration.addUrlPatterns("/admin/updatenews/*");
		registration.addUrlPatterns("/admin/contactus");
		registration.addUrlPatterns("/admin/training/*");
		registration.addUrlPatterns("/admin/training1/*");
		registration.addUrlPatterns("/admin/relatedlink");
		registration.addUrlPatterns("/admin/party");
		registration.addUrlPatterns("/admin/research-platform/*");
		registration.addUrlPatterns("/admin/add-research-platform");
		registration.addUrlPatterns("/admin/del-research-platform/*");
		registration.addUrlPatterns("/admin/update-research-platform/*");
		return registration;
	}

	@Bean
	public FilterRegistrationBean<MyContactUsFilter> myContactUsFilterRegistration() {
		FilterRegistrationBean<MyContactUsFilter> registration = new FilterRegistrationBean<MyContactUsFilter>();
		registration.setFilter(new MyContactUsFilter());
		registration.setName("myContactUsFilter");
		registration.addUrlPatterns("/");
		registration.addUrlPatterns("/index");
		registration.addUrlPatterns("/aboutus/*");
		registration.addUrlPatterns("/contactus");
		registration.addUrlPatterns("/detail/*");
		registration.addUrlPatterns("/services/*");
		registration.addUrlPatterns("/party");
		registration.addUrlPatterns("/partydetail/*");
		registration.addUrlPatterns("/news/*");
		registration.addUrlPatterns("/search/*");
		registration.addUrlPatterns("/training/*");
		registration.addUrlPatterns("/trainingdetail/*");
		registration.addUrlPatterns("/research-platform/*");
		return registration;
	}

	public class MyLoginFilter implements Filter {
		@Override
		public void destroy() {
		}

		@Override
		public void doFilter(ServletRequest sreq, ServletResponse srep, FilterChain filterChain)
				throws IOException, ServletException {
			HttpServletRequest req = (HttpServletRequest) sreq;
			HttpSession session = req.getSession();
			if (session.getAttribute("user") == null) {
//				((HttpServletResponse) srep).sendRedirect("/UESTC/admin/login");
				 ((HttpServletResponse) srep).sendRedirect("/admin/login");
			}
			filterChain.doFilter(sreq, srep);
		}

		@Override
		public void init(FilterConfig arg0) throws ServletException {
		}

	}

	public class MyContactUsFilter implements Filter {

		@Override
		public void destroy() {
		}

		@Override
		public void doFilter(ServletRequest sreq, ServletResponse srep, FilterChain filterChain)
				throws IOException, ServletException {
			HttpServletRequest req = (HttpServletRequest) sreq;
			HttpSession session = req.getSession();
			if (session.getAttribute("contactUs") == null) {
				ContactUsExample e1 = new ContactUsExample();
				session.setAttribute("contactUs", contactUsMapper.selectByExample(e1).get(0));
			}
			if (session.getAttribute("relatedLinks") == null) {
				RelatedLinkExample e2 = new RelatedLinkExample();
				session.setAttribute("relatedLinks", relatedLinkMapper.selectByExample(e2));
			}
			filterChain.doFilter(sreq, srep);
		}

		@Override
		public void init(FilterConfig arg0) throws ServletException {

		}

	}
}
